package com.gpframework.module.controller.module;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.redis.RedisCache;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.common.utils.aliySms.AliySmsUtils;
import com.gpframework.module.module.service.IGpCommonBannerService;
import com.gpframework.module.service.TokenService;
import com.gpframework.module.system.service.ISmsSendLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * gpframework
 * ==================================================================
 * CopyRight © 2017-2099 广州宏略信息科技有限公司
 * 官网地址：http://www.greatplan.com.cn
 * <p>
 * ------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下
 * 对本程序代码进行修改和使用；不允许对本程序代码以任何目的的再发布。
 * ==================================================================
 *
 * @ClassName LoginController
 * @Author Jesse(greatplan @ qq.com)
 * @Date 2020-11-06 11:00:00 星期五
 * @Version 1.0.0
 * @Description 登录相关
 */
@Api(tags = "登录相关")
@RequestMapping("/login")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Slf4j
public class LoginController {

    //    private final IApiLoginService apiLoginService;
    private final RedisCache redisCache;
    private final ISmsSendLogService iSmsSendLogService;

    @Autowired
    private TokenService tokenService;


    @Autowired
    private final IGpCommonBannerService iGpCommonBannerService;

    /**
     * 发送短信
     *
     * @param mobile
     * @param areaCode
     * @param type     1登录 2绑定
     * @return
     * @throws ClientException
     */
    @ResponseBody
    @ApiOperation(value = "发送短信")
    @RequestMapping(value = "/sendSms", method = RequestMethod.GET)
    public AjaxResult sendSms(@ApiParam(name = "mobile", value = "13800138000", required = true) String mobile,
                              @ApiParam(name = "areaCode", value = "123") String areaCode,
                              @ApiParam(name = "type", value = "1", required = true) String type) throws ClientException {
       /* // 手机号
        String mobile = request.getParameter("mobile");
        String areaCode = request.getParameter("areaCode");
        String type = request.getParameter("type");     // 1登录 2绑定*/
        if (StringUtils.isEmpty(mobile)) {
            return AjaxResult.parameterError();
        }
        // 18888888888作为测试账户不需要发送短信
        if ("18888888888".equals(mobile)) {
            return AjaxResult.success();
        }
        String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        String key;
        if ("1".equals(type)) {
            //登录
            SendSmsResponse res = AliySmsUtils.sendSms(mobile, randomCode, areaCode);
            if ("OK".equals(res.getCode())) {
                key = "cache_app_login:" + areaCode + mobile;
                redisCache.setCacheObject(key, randomCode);
                redisCache.expire(key, 3000);
                return AjaxResult.success("发送成功");
            } else {
                return AjaxResult.error("发送失败，请稍后重试");
            }
        } else if ("2".equals(type)) {
            //绑定
            SendSmsResponse res = AliySmsUtils.sendSms(mobile, randomCode, areaCode);
            if ("OK".equals(res.getCode())) {
                key = "cache_bind_mobile:" + areaCode + mobile;
                redisCache.setCacheObject(key, randomCode);
                redisCache.expire(key, 3000);
                return AjaxResult.success("发送成功");
            } else {
                return AjaxResult.error("发送失败，请稍后重试");
            }
        }
        return AjaxResult.parameterError();
    }
}
